package us.yesor.atlast;

import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;

public class ViewHistoryActivity extends ListActivity {
	// TODO
	// use shared preferences to track the current collection in use
	// pass as parameter when querying for selected items OR
	// bring up the Select Collection Activity
	private SimpleCursorAdapter adapter;
	private String activeItem;
	private String activeCollection;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list);
		activeItem = getSharedPreferences("prefs", Context.MODE_PRIVATE)
				.getString("activeItem", "No Item Selected");
		activeCollection = getSharedPreferences("prefs", Context.MODE_PRIVATE)
				.getString("activeCollection", "No Collection Selected");
		
		String[] projections = new String[] { InteractionTable.IID,
				InteractionTable.COLLECTION,
				InteractionTable.TIME, InteractionTable.ITEM,
				InteractionTable.TRIGGER, InteractionTable.VALUE,
				InteractionTable.STATE };
		
		String selection = InteractionTable.COLLECTION+" = ? AND " + InteractionTable.ITEM+" = ?";
		String[] selectionArgs = new String[] {activeCollection, activeItem};

		Cursor c = getContentResolver().query(
				Uri.parse(InteractionTable.CONTENT_URI+"/history"),
				projections,
				selection,
				selectionArgs, null);
		startManagingCursor(c);

		String[] columns = new String[] { InteractionTable.TIME,
				InteractionTable.TRIGGER, InteractionTable.VALUE,
				InteractionTable.STATE };

		int[] to = new int[] { R.id.time_entry, R.id.trigger_entry,
				R.id.value_entry, R.id.state_entry };

		// TODO deprecated SimpleCursorAdapter
		adapter = new SimpleCursorAdapter(this, R.layout.history_entry, c,
				columns, to);

		this.setListAdapter(adapter);
	}

}
